package org.soc.timesheet.domain;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import flexjson.ObjectBinder;
import flexjson.ObjectFactory;

@Entity
@Table(name = "SUB_PROJECTS")
@Configurable
@RooJavaBean
@RooJpaActiveRecord(versionField = "", table = "SUB_PROJECTS")
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { "timesheets", "usersProjects", "idProject" })
@RooJson
@JsonIgnoreProperties({ "timesheets", "usersProjects" })
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class SubProjects {

    public static SubProjects fromJsonToSubProjects(String json) {
        try {
            return new ObjectMapper().readValue(json, SubProjects.class);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static List<SubProjects> fromJsonArrayToSubProjectses(String json) {
        try {
            return new ObjectMapper().readValue(json, new TypeReference<List<SubProjects>>() {
            });
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new ArrayList<SubProjects>();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Integer id;

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public String toJson(String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(this);
    }

    public static String toJsonArray(Collection<SubProjects> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static String toJsonArray(Collection<SubProjects> collection, String[] fields) {
        return new JSONSerializer().include(fields).exclude("*.class").serialize(collection);
    }

    @OneToMany(mappedBy = "idSubProject")
    private Set<Timesheet> timesheets;

    @OneToMany(mappedBy = "idSubProject")
    private Set<UsersProject> usersProjects;

    @ManyToOne
    @JoinColumn(name = "ID_PROJECT", referencedColumnName = "ID", nullable = false)
    private Projects idProject;

    @Column(name = "DESC_SUB_PROJECT", length = 45)
    private String descSubProject;

    @Column(name = "START_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "MM")
    private Calendar startDate;

    @Column(name = "END_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "MM")
    private Calendar endDate;

    @Column(name = "CLOSED")
    private Boolean closed;

    public Set<Timesheet> getTimesheets() {
        return timesheets;
    }

    public void setTimesheets(Set<Timesheet> timesheets) {
        this.timesheets = timesheets;
    }

    public Set<UsersProject> getUsersProjects() {
        return usersProjects;
    }

    public void setUsersProjects(Set<UsersProject> usersProjects) {
        this.usersProjects = usersProjects;
    }

    public Projects getIdProject() {
        return idProject;
    }

    public void setIdProject(Projects idProject) {
        this.idProject = idProject;
    }

    public String getDescSubProject() {
        return descSubProject;
    }

    public void setDescSubProject(String descSubProject) {
        this.descSubProject = descSubProject;
    }

    public Calendar getStartDate() {
        return startDate;
    }

    public void setStartDate(Calendar startDate) {
        this.startDate = startDate;
    }

    public Calendar getEndDate() {
        return endDate;
    }

    public void setEndDate(Calendar endDate) {
        this.endDate = endDate;
    }

    public Boolean getClosed() {
        return closed;
    }

    public void setClosed(Boolean closed) {
        this.closed = closed;
    }

    public String toString() {
        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames("timesheets", "usersProjects", "idProject").toString();
    }

    @PersistenceContext
    transient EntityManager entityManager;

    public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("");

    public static final EntityManager entityManager() {
        EntityManager em = new SubProjects().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countSubProjectses() {
        return entityManager().createQuery("SELECT COUNT(o) FROM SubProjects o", Long.class).getSingleResult();
    }

    public static List<SubProjects> findAllSubProjectses() {
        return entityManager().createQuery("SELECT o FROM SubProjects o", SubProjects.class).getResultList();
    }

    public static List<SubProjects> findAllSubProjectses(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM SubProjects o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, SubProjects.class).getResultList();
    }

    public static SubProjects findSubProjects(Integer id) {
        if (id == null) return null;
        return entityManager().find(SubProjects.class, id);
    }

    public static List<SubProjects> findSubProjectsEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM SubProjects o", SubProjects.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    public static List<SubProjects> findSubProjectsEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM SubProjects o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, SubProjects.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            SubProjects attached = SubProjects.findSubProjects(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public SubProjects merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        SubProjects merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
}
